/*
 * MIT License
 *
 * Copyright (c) 2023 北京凯特伟业科技有限公司
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.je.bpm.api.runtime.process.impl;

import com.je.bpm.api.runtime.shared.model.impl.ModelConverter;
import com.je.bpm.core.model.FlowNode;
import com.je.bpm.core.model.config.task.TaskBasicConfigImpl;
import com.je.bpm.core.model.config.task.TaskFormBasicConfigImpl;
import com.je.bpm.core.model.event.EndEvent;
import com.je.bpm.core.model.task.KaiteBaseUserTask;
import com.je.bpm.model.shared.model.FormConfig;
import com.je.bpm.model.shared.model.impl.FormConfigImpl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class ApiFormConfigConverter implements ModelConverter<FlowNode, Map<String, Object>, FormConfig> {

    private ApiFormChildFuncConfigConverter apiFormChildFuncConfigConverter;
    private ApiFormButtonConfigConverter apiFormButtonConfigConverter;
    private ApiFormFieldConfigConverter apiFormFieldConfigConverter;

    public ApiFormConfigConverter(ApiFormButtonConfigConverter apiFormButtonConfigConverter,
                                  ApiFormFieldConfigConverter apiFormFieldConfigConverter,
                                  ApiFormChildFuncConfigConverter apiFormChildFuncConfigConverter) {
        this.apiFormButtonConfigConverter = apiFormButtonConfigConverter;
        this.apiFormFieldConfigConverter = apiFormFieldConfigConverter;
        this.apiFormChildFuncConfigConverter = apiFormChildFuncConfigConverter;
    }

    @Override
    public FormConfig from(FlowNode kaiteUserTask) {
        TaskFormBasicConfigImpl taskFormBasicConfig = null;
        TaskBasicConfigImpl basicConfig = null;
        if (kaiteUserTask instanceof KaiteBaseUserTask) {
            taskFormBasicConfig = ((KaiteBaseUserTask) kaiteUserTask).getTaskFormBasicConfig();
            basicConfig = ((KaiteBaseUserTask) kaiteUserTask).getTaskBasicConfig();
        }
        if (kaiteUserTask instanceof EndEvent) {
            taskFormBasicConfig = ((EndEvent) kaiteUserTask).getTaskFormBasicConfig();
        }
        FormConfig formConfig = new FormConfigImpl();
        formConfig.setFormButtonList(apiFormButtonConfigConverter.from(taskFormBasicConfig.getTaskFormButtonConfig()).getList());
        formConfig.setFormFieldList(apiFormFieldConfigConverter.from(taskFormBasicConfig.getTaskFormFieldConfig()).getList());
        formConfig.setFormChildFuncs(apiFormChildFuncConfigConverter.from(taskFormBasicConfig.getTaskFormChildFuncConfig()).getList());
        if (basicConfig != null) {
            formConfig.setFormEditable(basicConfig.getFormEditable());
            formConfig.setListSynchronization(basicConfig.getListSynchronization());
        }
        return formConfig;
    }

    @Override
    public FormConfig from(FlowNode source, Map<String, Object> Params) {
        return null;
    }

    @Override
    public List<FormConfig> from(Collection<FlowNode> sources) {
        return null;
    }

    @Override
    public List<FormConfig> from(Collection<FlowNode> sources, Map<String, Object> Params) {
        return null;
    }


}
